<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */

namespace Admin\Controller;

use Admin\Controller\AdminController;
use Tool\Api\AreaApi;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;

class ProvinceController extends AdminController{

	protected function _initialize(){
		parent::_initialize();
	}

	/**
	 * show province list
	 * @return [type] [description]
	 */
	public function index($p=1){
		$this->assignTitle('省份管理');
		$r = apiCall(ProvinceApi::QUERY,array(array(),array('curpage'=>$p,'size'=>10),'id desc'));
		// dump($r);exit;
		if($r['status']){
			$this->assign('list',$r['info']['list']);
			$this->assign('show',$r['info']['show']);
		}else{
			$this->error('系统错误： Province:query:error ');
		}
		$this->display('Area/index_p');
	}

	/**
	 * 删除省份 将删除子市、区
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function delete($id){
		$r = apiCall(ProvinceApi::DELETE_BY_ID,array($id));
		// dump($r);exit;

		if(!$r['status']) $this->error($r['info']);
		else $this->success('修改成功');
	}

	public function add(){
		if(IS_GET){
			$this->display('Area/add_p');
		}elseif(IS_AJAX){
			$map               = array();
			$map['provinceID'] = I('post.provinceID');
			$map['province']   = I('post.province');
			$map['countryid']  = 1017;
			$this->upd($map);
		}
	}

	public function edit($id){
		$r = apiCall(ProvinceApi::GET_INFO,array(array('id'=>$id)));
		if(!$r['status']) $this->error('ProvinceApi get error');
		if(!$r['info'])   $this->error('省份不存在');

		if(IS_GET){
			$this->assign('entry',$r['info']);
			$this->display('Area/edit_p');
		}elseif(IS_POST){
			$map               = array();
			$map['provinceID'] = I('post.provinceID');
			$map['province']   = I('post.province');
			$this->upd($map,$id);
		}
	}

	public function search($kword,$p=1){
		$this->assignTitle($kword.' - 搜索结果');
		$where = array();
		if($kword){
			$where['provinceID'] = array('like','%'.$kword.'%');
			$where['province']   = array('like','%'.$kword.'%');
			$where['_logic']     = 'OR';
		}
		$r = apiCall(ProvinceApi::QUERY,array($where,array('curpage'=>$p,'size'=>10),'id desc'));
		// echo M()->getLastSql();
		// dump($r);exit;
		if($r['status'])
		{
	    $this->assign('list',$r['info']['list']);
	    $this->assign('show',$r['info']['show']);
		}else{
	    $this->error('Province:Page:ERROR');
		}
		$this->display('Area/index_p');
	}

	protected function upd($map,$id=0){
		// dump($map);exit;
		if(!preg_match('/[\x{4e00}-\x{9fa5}]+/u', $map['province'])) $this->error('名称格式错误');
		if(!preg_match('/^[A-Za-z0-9]{2}0000$/', $map['provinceID'])) $this->error('编号格式错误');

		$where               = array();
		$where['provinceID'] = $map['provinceID'];
		$where['province']   = $map['province'];
		$where['_logic']     = 'OR';

		if($id){
			$where2['_complex'] = $where;
			$where2['id']       = array('neq',$id);
		}else{
			$where2 = $where;
		}
		$r = apiCall(ProvinceApi::GET_INFO,array($where2));
		unset($where);
		// dump(M()->getLastSql());exit;
		if(!$r['status']) $this->error('ProvinceApi get error');
		if($r['info']){
			if($r['info']['provinceid'] == $map['provinceID'])
				$this->error('该编号已存在');
			if($r['info']['province']   == $map['province']) $this->error('该名字已存在');
		}
		if($id){
			$r = apiCall(ProvinceApi::SAVE_BY_ID,array($id,$map));
			if(!$r['status']) $this->error('修改失败');
			$this->success('修改成功',U('Province/index'));
		}else{
			$r = apiCall(ProvinceApi::ADD,array($map));
			if(!$r['status']) $this->error('添加失败');
			$this->success('添加成功',U('Province/index'));
		}
	}
}